/*************************************************************************************
* 	 Copyright (C) 2010 by Information Systems Group, Saarland University  			*
*    http://infosys.cs.uni-saarland.de												*
* 	 																				*
* 	 This file is part of Hadoop++.												 	*
*																					*
*    Hadoop++ is free software: you can redistribute it and/or modify				*
*    it under the terms of the GNU Lesser General Public License as published by	*
*    the Free Software Foundation, either version 3 of the License, or				*
*    (at your option) any later version.											*
*																					*
*    Hadoop++ is distributed in the hope that it will be useful,					*
*    but WITHOUT ANY WARRANTY; without even the implied warranty of					*
*    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the					*
*    GNU Lesser General Public License for more details.							*
*																					*
*    You should have received a copy of the GNU Lesser General Public License		*
*    along with Hadoop++.  If not, see <http://www.gnu.org/licenses/>.				*
*************************************************************************************/
package unisb.cs.core.index.structure;

import unisb.cs.core.binary.utils.BinaryUtils;
import unisb.cs.data.types.DataTypes;

/**
 * This class manages the index header for CSS-tre having
 * record offsets as Long values. Consequently, the data offset
 * is now stored as Long value, as shown below:
 * 
 *  ________________________________________________________________________________________
 * |			  |				|				   |					|					|		
 * | Index Offset | Data Offset | Number of Tuples | First Key in Split | Last key in Split |
 * |  (4 bytes)   |	 (8 bytes)  |	 (4 bytes)	   |	  (key B)       |	   (key B)		|	
 * |______________|_____________|__________________|____________________|___________________|
 * 
 */
public class IndexHeaderWithLongOffsets extends IndexHeader{

	// record offset is Long => data offset should also be Long
	long dataEndOffset = 0;

	public IndexHeaderWithLongOffsets(int keyType, int keySize) {
		super(keyType, keySize);
		this.keySize = keySize + CSSTreeWithLongOffsets.OFFSET_BYTES;
	}

	/**
	 * Get the header size;
	 * data offset is a long now!
	 */
	public int getHeaderSize() {
		return 16 + 2 * keySize;
	}

	/**
	 * Parse the header bytes
	 */
	public void parse(byte[] header) {
		indexEndOffset = BinaryUtils.getInt(header, 0);
		dataEndOffset = BinaryUtils.getLong(header, 4);
		noOfTuples = BinaryUtils.getInt(header, 12);

		switch (keyType) {
		case DataTypes.INT_TYPE:
			firstIntKey = BinaryUtils.getInt(header, 16);
			lastIntKey = BinaryUtils.getInt(header, 16 + keySize);
			break;

		case DataTypes.FLOAT_TYPE:
			firstFloatKey = BinaryUtils.getFloat(header, 16);
			lastFloatKey = BinaryUtils.getFloat(header, 16 + keySize);
			break;

		case DataTypes.CHAR_TYPE:
			firstStringKey = BinaryUtils.getString(header, 16, keySize);
			lastStringKey = BinaryUtils.getString(header, 16 + keySize,
					actualKeySize);
			break;

		case DataTypes.DATE_TYPE:
			firstDateKey = BinaryUtils.getDate(header, 16, keySize);
			lastDateKey = BinaryUtils.getDate(header, 16 + keySize,
					actualKeySize);
			break;
		default:
			System.out.println("Key Type not resolved!");
		}
	}

	/**
	 * @return The data size as integer
	 */
	public long getLongDataSize() {
		return dataEndOffset;
	}
}
